package com.project.gjzb.schedule;


import com.project.gjzb.dto.BusLineStationDto;
import com.project.gjzb.dto.ParamDto;
import com.project.gjzb.entity.*;
import com.project.gjzb.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * - 定时任务
 */
@Slf4j
@Component
@EnableScheduling
public class Scheduler {

    @Resource
    BusLineService busLineService;
    @Resource
    BusLineStationService busLineStationService;
    @Resource
    BusVehicleService busVehicleService;
    @Resource
    BusDirectionService busDirectionService;
    @Resource
    EnterpriseService enterpriseService;

    @Scheduled(cron = "0 5 0 * * ?")
    private void syncJob() {
        log.info("每天0点5分定时任务执行开始");
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            LocalDate before = now.minusDays(1);
            ParamDto dto = new ParamDto();
            dto.setDate(formatter.format(before));

            log.info("【企业基本信息】" + "【" + formatter.format(before) + "】");
            List<TobjEnterprise> tobjEnterpriseList = enterpriseService.getList();
            if (tobjEnterpriseList.size() > 0) {
                enterpriseService.deleteByDate(dto);
                for (TobjEnterprise tobjEnterprise : tobjEnterpriseList) {
                    tobjEnterprise.setRq(dto.getDate());
                }
                enterpriseService.saveList(tobjEnterpriseList);
                List<Enterprise> enterpriseList = enterpriseService.getByDate(dto.getDate());
                enterpriseService.deleteAll();
                enterpriseService.insert(enterpriseList);
            }

            log.info("【站级首末班站点基础信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusLineStation> tobjBusLineStationList = busLineStationService.getList();
            if (tobjBusLineStationList.size() > 0) {
                busLineStationService.deleteByDate(dto);
                for (TobjBusLineStation tobjBusLineStation : tobjBusLineStationList) {
                    tobjBusLineStation.setRq(dto.getDate());
                }
                busLineStationService.saveList(tobjBusLineStationList);
                List<BusLineStation> busLineStationList = busLineStationService.getByDate(dto.getDate());
                busLineStationService.deleteAll();
                busLineStationService.insert(busLineStationList);
            }
            // 重复项删除
            List<BusLineStationDto> repeatList = busLineStationService.getRepeatAll();
            if (repeatList.size() > 0) {
                for (BusLineStationDto busLineStationDto : repeatList) {
                    List<BusLineStation> deleteList = busLineStationService.getByRepeat(busLineStationDto);
                    if (deleteList.size() > 0) {
                        for (int i = 1; i < deleteList.size(); i++) {
                            busLineStationService.deleteById(deleteList.get(i));
                        }
                    }
                }
            }
            // 更新特殊高峰线路时间
            List<TobjBusLineStation> firstList = busLineStationService.getFirst();
            List<TobjBusLineStation> maxList = busLineStationService.getMax();
            if (firstList.size() > 0) {
                for (TobjBusLineStation tobjBusLineStation : firstList) {
                    if (maxList.size() > 0) {
                        List<TobjBusLineStation> newList = maxList.stream().filter(item -> item.getBusline_id().equals(tobjBusLineStation.getBusline_id())).collect(Collectors.toList());
                        if (newList.size() > 0) {
                            if (!StringUtils.isEmpty(newList.get(0).getPeakendtime())) {
                                BusLineStation busLineStation1 = new BusLineStation();
                                busLineStation1.setId(tobjBusLineStation.getBusstop_id());
                                busLineStation1.setXlid(tobjBusLineStation.getBusline_id());
                                busLineStation1.setMbsj(newList.get(0).getPeakendtime());
                                busLineStationService.updateSzmbById(busLineStation1);
                            }
                        }
                    }
                }
            }
            if (maxList.size() > 0) {
                for (TobjBusLineStation tobjBusLineStation : maxList) {
                    BusLineStation busLineStation00 = new BusLineStation();
                    busLineStation00.setId(tobjBusLineStation.getBusstop_id());
                    busLineStation00.setXlid(tobjBusLineStation.getBusline_id());
                    busLineStation00.setSbsj(tobjBusLineStation.getPeakendtime());
                    busLineStation00.setZj(String.valueOf(tobjBusLineStation.getStationlevel()));
                    busLineStationService.updateMzsbById(busLineStation00);
                }
            }

            log.info("【线路走向信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusDirection> tobjBusDirectionList = busDirectionService.getList();
            if (tobjBusDirectionList.size() > 0) {
                busDirectionService.deleteByDate(dto);
                for (TobjBusDirection tobjBusDirection : tobjBusDirectionList) {
                    tobjBusDirection.setRq(dto.getDate());
                }
                busDirectionService.saveList(tobjBusDirectionList);
            }

            log.info("【线路基础信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusLine> tobjBusLineList = busLineService.getList();
            if (tobjBusLineList.size() > 0) {
                busLineService.deleteByDate(dto);
                for (TobjBusLine tobjBusLine : tobjBusLineList) {
                    tobjBusLine.setRq(dto.getDate());
                }
                busLineService.saveList(tobjBusLineList);
                List<BusLine> busLineList = busLineService.getByDate(dto.getDate());
                busLineService.deleteAll();
                busLineService.insert(busLineList);
            }

            log.info("【线路配车信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusVehicle> tobjBusVehicleList = busVehicleService.getList();
            if (tobjBusVehicleList.size() > 0) {
                busVehicleService.deleteByDate(dto);
                for (TobjBusVehicle tobjBusVehicle : tobjBusVehicleList) {
                    tobjBusVehicle.setRq(dto.getDate());
                }
                busVehicleService.saveList(tobjBusVehicleList);
                List<BusVehicle> busVehicleList = busVehicleService.getByDate(dto.getDate());
                busVehicleService.deleteAll();
                busVehicleService.insert(busVehicleList);
            }

        } catch (Exception e) {
            log.error("【每天0点5分定时任务执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("每天0点5分定时任务执行结束");
        }
    }
}
